---
title: "Supplemental Materials"
output: 
  pdf_document:
    keep_tex: yes
    toc: true
urlcolor: blue
geometry: "left=1.7cm,right= 1.7cm,top=1.7cm,bottom=1.7cm"
header-includes:
- \usepackage{booktabs}
- \usepackage{longtable}
---

```{r setup, echo = F, message = F, warning = F, collapse = T}
knitr::opts_chunk$set(echo = TRUE)
library(tidyverse)
library(knitr)
library(kableExtra)
library(stargazer)
library(sandwich)
library(Zelig)
library(gridExtra)
library(patchwork)
library(nnet)
library(Cairo)

# load data 
# set working directory:
data <- readRDS("main_sample.rds")

# make ResponseID a factor
data$ResponseID <- as.factor(data$ResponseID)

# function to compute/display cluster-robust SEs (clustered by ResponseID)
clSE <- function(model){
  cov <- vcovCL(model, cluster = ~ResponseID)
  robust.se <- sqrt(diag(cov))
  return(robust.se)
}
# function to compute/display new F statistic (clustered by ResponseID)
F_stat <- function(model){
  vcov <- vcovCL(model, cluster = ~ResponseID)
  F_val <- lmtest::waldtest(model, vcov = vcov)[2,3]
  p_val <- lmtest::waldtest(model, vcov = vcov)[2,4]
  stars <- case_when(p_val < 0.001 ~ "***",
                   p_val >= 0.001 & p_val < 0.01 ~ "**",
                   p_val >= 0.01 & p_val < 0.05 ~ "*",
                   TRUE ~ "")
  df_diff <- (lmtest::waldtest(model, vcov = vcov)[2,2])*-1
  df <- lmtest::waldtest(model, vcov = vcov)[1,1] 
  out<- paste(round(F_val, 3), stars, " (df = ", df_diff, "; ", df, ")", sep = "")
  return(out)
}


# reorder levels of largest group, omitted group and R race variables
data$largest_group <- fct_relevel(data$largest_group,
                                  c("White",
                                    "Latino",
                                    "Asian",
                                    "Black"))
data$omitted_group <- fct_relevel(data$omitted_group,
                                  c("White",
                                    "Latino",
                                    "Asian",
                                    "Black"))
data$race <- fct_relevel(data$race,
                         c("White",
                           "Hispanic/Latino(a)",
                           "Asian/Asian American",
                           "Black/African American"))
```

\newpage 

# Survey Experiment Implementation: Diagnostic Tests

## Carryover Effects

```{r message = F, results = "asis", echo = F, eval = F}
carryovers_largest_group <- lm(rating ~ composition*task_no + largest_group*task_no, data) 
carryovers_omitted_group <- lm(rating ~ composition*task_no + omitted_group*task_no, data) 
   
stargazer(carryovers_largest_group, carryovers_omitted_group,
          title = "Carryover Effects",
          se = list(clSE(carryovers_largest_group), clSE(carryovers_omitted_group)), 
          header = F, digits = 3, 
          t.auto = F, p.auto = F,
          no.space = T, single.row = T, 
          star.cutoffs = c(0.05, 0.01, 0.001),
          order = c(1:4, 6:11, 5, 12:22),
          dep.var.labels.include = F,
          dep.var.caption = "",
          intercept.bottom = T,
          covariate.labels = c("60/38/2", 
                               "70/28/2",
                               "80/18/2",
                               "90/8/2",
                               "Latinos",
                               "Asians",
                               "Blacks",
                               "Latinos",
                               "Asians",
                               "Blacks",
                               "Task Number",
                               "60/38/2 * Task Number",
                               "70/28/2 * Task Number",
                               "80/18/2 * Task Number",
                               "90/8/2 * Task Number",
                               "Latino largest * Task Number",
                               "Asian largest * Task Number",
                               "Black largest * Task Number",
                               "Latino absent * Task Number",
                               "Asian absent * Task Number",
                               "Black absent * Task Number"),
           omit.stat = "f",
           add.lines = list(c("F Statistic", F_stat(carryovers_largest_group), F_stat(carryovers_omitted_group))))
``` 

\newpage

## Profile Order Effects

```{r, collapse = T, message = F, results = "asis", echo = F, eval = F}
profile_largest_group <- lm(rating ~ composition*neighborhood + largest_group*neighborhood, data) 
profile_omitted_group <- lm(rating ~ composition*neighborhood + omitted_group*neighborhood, data) 
   
  
stargazer(profile_largest_group, profile_omitted_group,
          title = "Profile Order Effects",
          se = list(clSE(profile_largest_group), clSE(profile_omitted_group)), 
          header = F, digits = 3, 
          t.auto = F, p.auto = F,
          no.space = T, single.row = T, 
          star.cutoffs = c(0.05, 0.01, 0.001),
          order = c(1:4, 6:11, 5, 12:22),
          dep.var.labels.include = F,
          dep.var.caption = "",
          intercept.bottom = T,
          covariate.labels = c("60/38/2", 
                               "70/28/2",
                               "80/18/2",
                               "90/8/2",
                               "Latinos",
                               "Asians",
                               "Blacks",
                               "Latinos",
                               "Asians",
                               "Blacks",
                               "Second Profile in set",
                               "60/38/2 * 2nd profile",
                               "70/28/2 * 2nd profile",
                               "80/18/2 * 2nd profile",
                               "90/8/2 * 2nd profile",
                               "Latino largest * 2nd profile",
                               "Asian largest * 2nd profile",
                               "Black largest * 2nd profile",
                               "Latino absent * 2nd profile",
                               "Asian absent * 2nd profile",
                               "Black absent * 2nd profile"),
           omit.stat = "f",
           add.lines = list(c("F Statistic", F_stat(profile_largest_group), F_stat(profile_omitted_group))))
``` 

\newpage

### Heterogeneity, Largest Group, and Absent Group Conditions and Participant Characteristics 

Profile attributes and joint distribution of participant characteristics 

```{r, echo = F, results = "hide"}
composition_test <- multinom(composition ~ race + age + gender + educ + inc_num + born + aa_factor + immigration_factor, data = data)
largest_group_test <- multinom(largest_group ~ race + age + gender + educ + inc_num + born + aa_factor + immigration_factor , data = data)
absent_group_test <- multinom(omitted_group ~ race + age  +gender + educ + inc_num + born + aa_factor + immigration_factor, data = data)
```

```{r, echo = F, message = F, warning = F, results = "asis"}
stargazer(composition_test,
          title = "Heterogeneity Treatment Conditions on Participant Characteristics",
          header = F, digits = 3, 
          t.auto = F, p.auto = F,
          no.space = T, single.row = T, 
          star.cutoffs = c(0.05, 0.01, 0.001),
          dep.var.caption = "Heterogeneity (Ref = 50/48/2)",
          intercept.bottom = T,
          covariate.labels = c("Latino",
                               "Asian",
                               "Black",
                               "Age",
                               "Male",
                               "High school/GED",
                               "2-year degree (Associate's)",
                               "Some college",
                               "4-year degree (Bachelor's)",
                               "Graduate or professional degree",
                               "Family Income (in thousands)",
                               "Born in U.S. territory (e.g. Puerto Rico)",
                               "Born in some other country",
                               "Weakly Oppose Aff. Action",
                               "Neither favor nor oppose Aff. Action",
                               "Weakly Favor Aff. Action",
                               "Strongly Favor Aff. Action", 
                               "Immigration should be kept at present level", 
                               "Immigration should be increased"))
```

\newpage

```{r, echo = F, message = F, warning = F, results = "asis"}
stargazer(largest_group_test,
          title = "Largest Group Treatment Conditions and Participant Characteristics",
          header = F, digits = 3, 
          t.auto = F, p.auto = F,
          no.space = T, single.row = T, 
          star.cutoffs = c(0.05, 0.01, 0.001),
          dep.var.caption = "Largest Group (Ref = Whites)",
          intercept.bottom = T,
          covariate.labels = c("Latino",
                               "Asian",
                               "Black",
                               "Age",
                               "Male",
                               "High school/GED",
                               "2-year degree (Associate's)",
                               "Some college",
                               "4-year degree (Bachelor's)",
                               "Graduate or professional degree",
                               "Family Income (in thousands)",
                               "Born in U.S. territory (e.g. Puerto Rico)",
                               "Born in some other country",
                               "Weakly Oppose Aff. Action",
                               "Neither favor nor oppose Aff. Action",
                               "Weakly Favor Aff. Action",
                               "Strongly Favor Aff. Action", 
                               "Immigration should be kept at present level", 
                               "Immigration should be increased")) 
```
\newpage

```{r, echo = F, message = F, warning = F, results = "asis"}
stargazer(absent_group_test,
          title = "Absent Group Treatment Conditions and Participant Characteristics",
          header = F, digits = 3, 
          t.auto = F, p.auto = F,
          no.space = T, single.row = T, 
          star.cutoffs = c(0.05, 0.01, 0.001),
          dep.var.caption = "Absent Group (Ref = Whites)",
          intercept.bottom = T,
          covariate.labels = c("Latino",
                               "Asian",
                               "Black",
                               "Age",
                               "Male",
                               "High school/GED",
                               "2-year degree (Associate's)",
                               "Some college",
                               "4-year degree (Bachelor's)",
                               "Graduate or professional degree",
                               "Family Income (in thousands)",
                               "Born in U.S. territory (e.g. Puerto Rico)",
                               "Born in some other country",
                               "Weakly Oppose Aff. Action",
                               "Neither favor nor oppose Aff. Action",
                               "Weakly Favor Aff. Action",
                               "Strongly Favor Aff. Action", 
                               "Immigration should be kept at present level", 
                               "Immigration should be increased")) 

```

\newpage

# Sample Descriptive Statistics

```{r, echo = F, results = 'as.is', message = F, warning = F}
# turn back into one row per respondent
wide <- data %>% select(ResponseID, age, inc_num, race, gender, educ, born,
                        aa_factor, immigration_factor) %>%
  mutate(dupeRID = duplicated(ResponseID)) %>%
  filter(dupeRID == FALSE) %>%
  select(-dupeRID)
# subset by race
wide_black <- wide %>% filter(race == "Black/African American") %>% select(-race)
wide_white <- wide %>% filter(race == "White") %>% select(-race)
wide_hispanic <- wide %>% filter(race == "Hispanic/Latino(a)") %>% select(-race)
wide_asian <- wide %>% filter(race == "Asian/Asian American") %>% select(-race)

# cross tab all factor variables 
## whole dataset (responseID is in col 1, age in col2, inc_num in col 3, race in col4) 
summary_vector <- unlist(lapply(wide[ , 5:9], function(Variable) format(round(prop.table(table(Variable))*100, 1), nsmall = 1)))
summary <- data.frame(Characteristic = names(summary_vector), Overall = summary_vector )
rownames(summary) <- NULL
# black
summary_blackv <- unlist(lapply(wide_black[ ,4:8], function(Variable) format(round(prop.table(table(Variable))*100, 1), nsmall = 1)))
summary_black <- data.frame(Characteristic = names(summary_blackv), Black = summary_blackv )
rownames(summary_black) <- NULL
# white
summary_whitev <- unlist(lapply(wide_white[ ,4:8], function(Variable) format(round(prop.table(table(Variable))*100, 1), nsmall = 1)))
summary_white <- data.frame(Characteristic = names(summary_whitev), White = summary_whitev )
rownames(summary_white) <- NULL
# hispanic
summary_hispv <- unlist(lapply(wide_hispanic[ ,4:8], function(Variable) format(round(prop.table(table(Variable))*100, 1), nsmall = 1)))
summary_hisp <- data.frame(Characteristic = names(summary_hispv), Hispanic = summary_hispv )
rownames(summary_hisp) <- NULL
# asian
summary_asianv <- unlist(lapply(wide_asian[ ,4:8], function(Variable) format(round(prop.table(table(Variable))*100, 1), nsmall = 1)))
summary_asian <- data.frame(Characteristic = names(summary_asianv), Asian = summary_asianv )
rownames(summary_asian) <- NULL
# merge
summary_table <- left_join(summary, summary_black, by = "Characteristic") %>%
  left_join(., summary_white, by = "Characteristic") %>%
    left_join(., summary_hisp, by = "Characteristic")  %>%
    left_join(., summary_asian, by = "Characteristic")  
summary_table$Characteristic <- as.character(summary_table$Characteristic)

# clean up characteristic names
summary_table$Characteristic <- sub(".*?\\.", "", summary_table$Characteristic)
summary_table$Characteristic[17] <- "Should be decreased"
summary_table$Characteristic[18] <- "Should be kept at present level"
summary_table$Characteristic[19] <- "Should be increased"

# change into character
summary_table <- data.frame(lapply(summary_table, as.character), stringsAsFactors = F)

# add rows for age and income ()
age_row <- c("Mean (sd)", 
             paste(round(mean(wide$age), 1),"(",round(sd(wide$age), 1),")"), 
             paste(round(mean(wide_black$age), 1),"(",format(round(sd(wide_black$age), 1), nsmall = 1),")"),
             paste(round(mean(wide_white$age), 1),"(",round(sd(wide_white$age), 1),")"),
             paste(round(mean(wide_hispanic$age),1),"(",round(sd(wide_hispanic$age),1),")"),
             paste(round(mean(wide_asian$age), 1),"(",round(sd(wide_asian$age), 1),")")) 
inc_row <- c("Mean (sd)", 
             paste(round(mean(wide$inc_num), 1),"(",format(round(sd(wide$inc_num), 1), nsmall = 1),")"), 
             paste(round(mean(wide_black$inc_num), 1),"(",round(sd(wide_black$inc_num), 1),")"),
             paste(format(round(mean(wide_white$inc_num), 1), nsmall = 1),"(",round(sd(wide_white$inc_num), 1),")"),
             paste(round(mean(wide_hispanic$inc_num),1),"(",round(sd(wide_hispanic$inc_num),1),")"),
             paste(round(mean(wide_asian$inc_num), 1),"(",round(sd(wide_asian$inc_num), 1),")")) 
summary_table2 <- rbind(age_row, 
      summary_table[1:8, ],
      inc_row,
      summary_table[9:19, ])

# remove column and row names for print
colnames(summary_table2) <- NULL
rownames(summary_table2) <- NULL

# print
kable(summary_table2, "latex", 
      booktabs = T, longtable = T,
      caption = "Descriptive Statistics",
      align = c("lccccc")) %>%
  kable_styling(position = "center",
                latex_options = c("repeat_header")) %>% 
  add_header_above(c(" ", 
                     "Full Sample \n(N = 1,803)" = 1, 
                     "Black Rs \n(N = 450)" = 1,
                     "White Rs \n(N = 449)" = 1,
                     "Hispanic Rs \n(N = 455)" = 1,
                     "Asian Rs \n(N = 449)" = 1),
                   bold = T) %>%
  group_rows("Age", 1, 1) %>%
  group_rows("Gender", 2, 3) %>%
  group_rows("Education", 4, 9) %>%
  group_rows("Family Income (in thousands)", 10, 10) %>%
  group_rows("Place of Birth", 11, 13) %>%
  group_rows("Affirmative Action Attitudes", 14, 18) %>%
  group_rows("Immigration Attitudes", 19, 21) %>%
  column_spec(1, width = "5.7cm") 
```

\newpage

# Descriptive Results

## Bivariate

```{r, echo = F, message = F, warning = F}
# Heterogeneity
het_bivariate <- data %>% group_by(composition) %>% 
                          summarise(Mean = mean(rating),
                                    total = n(),
                                    SE = sd(rating)/sqrt(total)) 

figS1a <- ggplot(het_bivariate,
                aes(x = fct_rev(composition), y = Mean, group = 1)) +
  geom_point(stat = "summary", 
             fun.y = sum, 
             size = 1) + 
  geom_errorbar(aes(ymin = Mean - qnorm(0.975)*SE, ymax = Mean + qnorm(0.975)*SE), width = 0.2) +
  stat_summary(fun.y = sum, geom = "line") +
  theme_bw() +
  ylim(2, 5) + 
  labs(y = "Perceived Diversity", x = "Neighborhood Distribution") +
  theme(axis.text=element_text(size=6),
        axis.title=element_text(size=7))

# Largest Group
largest_bivariate <- data %>% group_by(largest_group) %>% 
                          summarise(Mean = mean(rating),
                                    total = n(),
                                    SE = sd(rating)/sqrt(total)) 

figS1b <- ggplot(largest_bivariate,
                aes(x = largest_group, y = Mean, group = 1)) +
  geom_point(stat = "summary", 
             fun.y = sum, 
             size = 1) + 
  geom_errorbar(aes(ymin = Mean - qnorm(0.975)*SE, ymax = Mean + qnorm(0.975)*SE), width = 0.2) +
  theme_bw() +
  ylim(2, 5) + 
  labs(y = "Perceived Diversity", x = "Largest Group") +
  theme(axis.text=element_text(size=6),
        axis.title=element_text(size=7))

# Omitted Group
omitted_bivariate <- data %>% group_by(omitted_group) %>% 
                          summarise(Mean = mean(rating),
                                    total = n(),
                                    SE = sd(rating)/sqrt(total)) 

figS1c <- ggplot(omitted_bivariate,
                aes(x = omitted_group, y = Mean, group = 1)) +
  geom_point(stat = "summary", 
             fun.y = sum, 
             size = 1) + 
  geom_errorbar(aes(ymin = Mean - qnorm(0.975)*SE, ymax = Mean + qnorm(0.975)*SE), width = 0.2) +
  theme_bw() +
  ylim(2, 5) + 
  labs(y = "Perceived Diversity", x = "Absent Group") +
  theme(axis.text=element_text(size=6),
        axis.title=element_text(size=7))

figS1 <- figS1a + figS1b + figS1c

figS1

ggsave("bivariate.pdf", figS1, width = 22, height = 14 , units = "cm", dpi = 600)
```

## Means by Heterogeneity and Identity of Largest/Absent Groups

```{r, echo = F, message = F, warning = F}
# Heterogeneity and Largest Group
het_largest_means <- data %>% group_by(composition, largest_group) %>% 
                          summarise(Mean = mean(rating),
                                    total = n(),
                                    SE = sd(rating)/sqrt(total)) 

het_largest <- ggplot(het_largest_means,
                aes(x = fct_rev(composition), y = Mean, group = largest_group)) +
  geom_point(aes(color = largest_group),
             stat = "summary", 
             fun.y = sum, 
             size = 1,
             position = position_dodge(-0.3)) + 
  geom_errorbar(aes(ymin = Mean - qnorm(0.975)*SE, ymax = Mean + qnorm(0.975)*SE,
                    color = largest_group), 
                width = 0.2,
                position = position_dodge(-0.3)) +
  stat_summary(fun.y = sum, geom = "line",
               aes(color = largest_group),
               position = position_dodge(-0.3)) +
  theme_bw() +
  ylim(2, 5) + 
  labs(y = "Perceived Diversity", x = "Neighborhood Distribution",
              color = "Largest Group") +
  theme(legend.position="bottom", 
        axis.text=element_text(size=6),
        axis.title=element_text(size=7),
        legend.text=element_text(size=6),
        legend.title=element_text(size=7),
        legend.margin = margin(t = -0.05, unit='cm'),
        legend.key.height = unit(0.2, "cm")) +
  guides(color = guide_legend(title.position="top", 
                              title.hjust = 0.5, 
                              nrow = 2,
                              byrow = TRUE))

# Heterogeneity and Absent Group
het_absent_means <- data %>% group_by(composition, omitted_group) %>% 
                          summarise(Mean = mean(rating),
                                    total = n(),
                                    SE = sd(rating)/sqrt(total)) 

het_absent <- ggplot(het_absent_means,
                aes(x = fct_rev(composition), y = Mean, group = omitted_group)) +
  geom_point(aes(color = omitted_group),
             stat = "summary", 
             fun.y = sum, 
             size = 1,
             position = position_dodge(-0.3)) + 
  geom_errorbar(aes(ymin = Mean - qnorm(0.975)*SE, ymax = Mean + qnorm(0.975)*SE,
                    color = omitted_group), 
                width = 0.2,
                position = position_dodge(-0.3)) +
  stat_summary(fun.y = sum, geom = "line",
               aes(color = omitted_group),
               position = position_dodge(-0.3)) +
  theme_bw() +
  ylim(2, 5) + 
  labs(y = "Perceived Diversity", x = "Neighborhood Distribution",
              color = "Absent Group") +
  theme(legend.position="bottom", 
        axis.text=element_text(size=6),
        axis.title=element_text(size=7),
        legend.text=element_text(size=6),
        legend.title=element_text(size=7),
        legend.margin = margin(t = -0.05, unit='cm'),
        legend.key.height = unit(0.2, "cm")) +
  guides(color = guide_legend(title.position="top", 
                              title.hjust = 0.5, 
                              nrow = 2,
                              byrow = TRUE))

het_identity <- het_largest + het_absent 

het_identity

ggsave("het_identity_means.pdf", het_identity, width = 11, height = 10, units = "cm", dpi = 600, device=cairo_pdf)
```

\newpage

# Regression Results

## Heterogeneity and Largest Group
```{r, collapse = T, results = "asis", message = F, echo = F, eval = F}
model1 <- lm(rating ~ composition + largest_group, data)
# with controls
model1_controls <- lm(rating ~ composition + largest_group + race +
                        age + gender + educ + inc_num + born, data)

# table
stargazer(model1, model1_controls,
          title = "Heterogeneity + Largest Group",
          se = list(clSE(model1), clSE(model1_controls)),
          header = F, digits = 3,
          t.auto = F, p.auto = F, no.space = T, single.row = T,
          star.cutoffs = c(0.05, 0.01, 0.001),
          covariate.labels = c("60/38/2", 
                               "70/28/2",
                               "80/18/2",
                               "90/8/2",
                               "Latinos",
                               "Asians",
                               "Blacks",
                               "Latino",
                               "Asian",
                               "Black",
                               "Age",
                               "Male",
                               "High school/GED",
                               "2-year degree (Associate's)",
                               "Some college",
                               "4-year degree (Bachelor's)",
                               "Graduate or professional degree",
                               "Family Income (in thousands)",
                               "Born in U.S. territory (e.g. Puerto Rico)",
                               "Born in some other country"),
          omit.stat = "f",
          add.lines = list(c("F Statistic", F_stat(model1), F_stat(model1_controls))))
```


\newpage
## Heterogeneity and Absent Group

```{r, collapse = T, message = F, results = "asis", echo = F, eval = F}
model2 <- lm(rating ~ composition + omitted_group, data)
# with controls
model2_controls <- lm(rating ~ composition + omitted_group + race +
                        age + gender + educ + inc_num + born, data)

stargazer(model2, model2_controls,
          title = "Heterogeneity + Omitted Group",
          se = list(clSE(model2), clSE(model2_controls)),
          header = F, digits = 3, intercept.bottom = T, 
          t.auto = F, p.auto = F, no.space = T, single.row = T,
          star.cutoffs = c(0.05, 0.01, 0.001),
          covariate.labels = c("60/38/2", 
                               "70/28/2",
                               "80/18/2",
                               "90/8/2",
                               "Latinos",
                               "Asians",
                               "Blacks",
                               "Latino",
                               "Asian",
                               "Black",
                               "Age",
                               "Male",
                               "High school/GED",
                               "2-year degree (Associate's)",
                               "Some college",
                               "4-year degree (Bachelor's)",
                               "Graduate or professional degree",
                               "Family Income (in thousands)",
                               "Born in U.S. territory (e.g. Puerto Rico)",
                               "Born in some other country"),
          omit.stat = "f",
          add.lines = list(c("F Statistic", F_stat(model2), F_stat(model2_controls))))
```

\newpage
## Heterogeneity and Largest Group, Interacted by Participant Race

```{r, collapse = T, message = F, results = "asis", echo = F, eval = F}
# no controls
model3 <- lm(rating ~ composition*race + largest_group*race, data)
# with controls
model3_b <- lm(rating ~ composition*race + largest_group*race + 
                        age + gender + educ + inc_num + born, data)

stargazer(model3, model3_b,
          title = "Heterogeneity + Largest Group, Interacted by Participant Race",
          se = list(clSE(model3), clSE(model3_b)), 
          header = F, digits = 3, 
          t.auto = F, p.auto = F,
          no.space = T, single.row = T, 
          star.cutoffs = c(0.05, 0.01, 0.001),
          order = c(1:4, 8:10, 5:7, 21:41, 11:20, 42),
          dep.var.labels.include = F,
          dep.var.caption = "",
          intercept.bottom = T,
          covariate.labels = c("60/38/2", 
                               "70/28/2",
                               "80/18/2",
                               "90/8/2",
                               "Latinos",
                               "Asians",
                               "Blacks",
                               "Latino",
                               "Asian",
                               "Black",
                               "60/38/2 * Latino respondent",
                               "70/28/2 * Latino respondent",
                               "80/18/2 * Latino respondent",
                               "90/8/2 * Latino respondent",
                               "60/38/2 * Asian respondent",
                               "70/28/2 * Asian respondent",
                               "80/18/2 * Asian respondent",
                               "90/8/2 * Asian respondent",
                               "60/38/2 * Black respondent",
                               "70/28/2 * Black respondent",
                               "80/18/2 * Black respondent",
                               "90/8/2 * Black respondent",
                               "Latino largest * Latino respondent",
                               "Latino largest * Asian respondent",
                               "Latino largest * Black respondent",
                               "Asian largest * Latino respondent",
                               "Asian largest * Asian respondent",
                               "Asian largest * Black respondent",
                               "Black largest * Latino respondent",
                               "Black largest * Asian respondent",
                               "Black largest * Black respondent",
                               "Age",
                               "Male",
                               "High school/GED",
                               "2-year degree (Associate's)",
                               "Some college",
                               "4-year degree (Bachelor's)",
                               "Graduate or professional degree",
                               "Family Income (in thousands)",
                               "Born in U.S. territory (e.g. Puerto Rico)",
                               "Born in some other country"),
          omit.stat = "f",
          add.lines = list(c("F Statistic", F_stat(model3), F_stat(model3_b))))
```

\newpage

## Heterogeneity and Absent Group, Interacted by Participant Race

```{r, collapse = T, message = F, results = "asis", echo = F, eval = F}
# no controls
model4 <- lm(rating ~ composition*race + omitted_group*race, data)
# with controls
model4_b <- lm(rating ~ composition*race + omitted_group*race + 
                        age + gender + educ + inc_num + born, data)

stargazer(model4, model4_b,
          title = "Heterogeneity + Absent Group, Interacted by Participant Race",
          se = list(clSE(model4), clSE(model4_b)), 
          header = F, digits = 3, 
          t.auto = F, p.auto = F,
          no.space = T, single.row = T, 
          star.cutoffs = c(0.05, 0.01, 0.001),
          order = c(1:4, 8:10, 5:7, 21:41, 11:20, 42),
          dep.var.labels.include = F,
          dep.var.caption = "",
          intercept.bottom = T,
          covariate.labels = c("60/38/2", 
                               "70/28/2",
                               "80/18/2",
                               "90/8/2",
                               "Latinos ",
                               "Asians ",
                               "Blacks ",
                               "Latino",
                               "Asian",
                               "Black",
                               "60/38/2 * Latino respondent",
                               "70/28/2 * Latino respondent",
                               "80/18/2 * Latino respondent",
                               "90/8/2 * Latino respondent",
                               "60/38/2 * Asian respondent",
                               "70/28/2 * Asian respondent",
                               "80/18/2 * Asian respondent",
                               "90/8/2 * Asian respondent",
                               "60/38/2 * Black respondent",
                               "70/28/2 * Black respondent",
                               "80/18/2 * Black respondent",
                               "90/8/2 * Black respondent",
                               "Latinos absent * Latino respondent",
                               "Latinos absent * Asian respondent",
                               "Latinos absent * Black respondent",
                               "Asians absent * Latino respondent",
                               "Asians absent * Asian respondent",
                               "Asians absent * Black respondent",
                               "Blacks absent * Latino respondent",
                               "Blacks absent * Asian respondent",
                               "Blacks absent * Black respondent",
                               "Age",
                               "Male",
                               "High school/GED",
                               "2-year degree (Associate's)",
                               "Some college",
                               "4-year degree (Bachelor's)",
                               "Graduate or professional degree",
                               "Family Income (in thousands)",
                               "Born in U.S. territory (e.g. Puerto Rico)",
                               "Born in some other country"),
          omit.stat = "f",
          add.lines = list(c("F Statistic", F_stat(model4), F_stat(model4_b))))
```

\newpage

## Whites' Affirmative Action Attitudes and Identity of Largest Group

```{r, collapse = T, message = F, results = "asis", echo = F, eval = F}
# create whites only dataset
whites <- filter(data, race == "White")

# no controls model 
model6b <- lm(rating~ composition + aa_factor*largest_group, data = whites)
# controls
model6b_controls <- lm(rating~ composition + aa_factor*largest_group +
                       age + gender + educ + inc_num + born, data = whites)

stargazer(model6b, model6b_controls,
          title = "Whites' Affirmative Action Attitudes and Identity of Largest Group",
          se = list(clSE(model6b), clSE(model6b_controls)), 
          header = F, digits = 3, 
          t.auto = F, p.auto = F,
          no.space = T, single.row = T,
          star.cutoffs = c(0.05, 0.01, 0.001),
          order = c(1:4, 9:11, 5:8, 22:33, 12:21, 34),
          dep.var.labels.include = F,
          dep.var.caption = "",
          intercept.bottom = T,
          covariate.labels = c("60/38/2", 
                               "70/28/2",
                               "80/18/2",
                               "90/8/2",
                               "Latinos",
                               "Asians",
                               "Blacks",
                               "Weakly oppose Aff.Action ",
                               "Neither favor nor oppose Aff.Action",
                               "Weakly favor Aff.Action",
                               "Strongly favor Aff.Action",
                               "Latino largest * Weakly oppose Aff. Action",
                               "Latino largest * Neither favor nor oppose Aff.Action",
                               "Latino largest * Weakly favor Aff.Action",
                               "Latino largest * Strongly favor Aff.Action",
                               "Asian largest * Weakly oppose Aff. Action",
                               "Asian largest * Neither favor nor oppose Aff.Action",
                               "Asian largest * Weakly favor Aff.Action",
                               "Asian largest * Strongly favor Aff.Action",
                               "Black largest * Weakly oppose Aff. Action",
                               "Black largest * Neither favor nor oppose Aff.Action",
                               "Black largest * Weakly favor Aff.Action",
                               "Black largest * Strongly favor Aff.Action",
                               "Age",
                               "Male",
                               "High school/GED",
                               "2-year degree (Associate's)",
                               "Some college",
                               "4-year degree (Bachelor's)",
                               "Graduate or professional degree",
                               "Family Income (in thousands)",
                               "Born in U.S. territory (e.g. Puerto Rico)",
                               "Born in some other country"),
          omit.stat = "f",
          add.lines = list(c("F Statistic", F_stat(model6b), F_stat(model6b_controls))))
```

## Whites' Immigration Attitudes and Identity of the Largest Group

```{r, collapse = T, message = F, results = "asis", echo = F, eval = F}
# no controls model 
model7b <- lm(rating~ composition + immigration_factor*largest_group, data = whites)
# controls
model7b_controls <- lm(rating~ composition + immigration_factor*largest_group +
                       age + gender + educ + inc_num + born, data = whites)

stargazer(model7b, model7b_controls,
          title = "Whites' Immigration Attitudes and Race of Largest Group",
          se = list(clSE(model7b), clSE(model7b_controls)), 
          header = F, digits = 3, 
          t.auto = F, p.auto = F,
          no.space = T, single.row = T,
          star.cutoffs = c(0.05, 0.01, 0.001),
          order = c(1:4, 7:9, 5:6, 20:25, 10:19, 26),
          dep.var.labels.include = F,
          dep.var.caption = "",
          intercept.bottom = T,
          covariate.labels = c("60/38/2", 
                               "70/28/2",
                               "80/18/2",
                               "90/8/2",
                               "Latinos",
                               "Asians",
                               "Blacks",
                               "Keep present level of immigration",
                               "Immigration should be increased",
                               "Latino largest * Present Level Immigration",
                               "Latino largest * Immigration should be increased",
                               "Asian largest * Present Level Immigration",
                               "Asian largest * Immigration should be increased",
                               "Black largest * Present Level Immigration",
                               "Black largest * Immigration should be increased",
                               "Age",
                               "Male",
                               "High school/GED",
                               "2-year degree (Associate's)",
                               "Some college",
                               "4-year degree (Bachelor's)",
                               "Graduate or professional degree",
                               "Family Income (in thousands)",
                               "Born in U.S. territory (e.g. Puerto Rico)",
                               "Born in some other country"),
          omit.stat = "f",
          add.lines = list(c("F Statistic", F_stat(model7b), F_stat(model7b_controls))))
```

\newpage

# Earlier Data Collection

See code and data in "02 Earlier Data Collection Referenced in Supplemental Materials" folder. 


\newpage

# Forced Choice (AMCEPs)

Note: Unlike linear models, AMCEP models (like AMCEs) do not include respondent-level control variables since vignette attributes are randomly assigned. For models where respondent characteristics (like race or attitudes towards AA and immigration), I subsetted the sample and ran the models separately on each segment. AMCEP models can estimate the difference in contrasting selection probabilities between two different respondent subgroups, but not the AMCEP and not for more than two subgroups.

The AMCEP estimand was developed by Flavien Ganter, who generously shared his code with his. Our code calls a separate R script that contains the function for calculating AMCEPs.

## Heterogeneity and Largest Group 

```{r, echo = F, warning = F, message = F}
# run "function_amcep_v6.R" 
# insert your working directory accordingly
source("~/function_amcep_v6.R") 
```

```{r, echo = F, warning = F, message = F, fig.align= "center", fig.width = 3.75, fig.height = 4.75}
# estimate and store coefficients in dataframe
model1_amcep <- data.frame(summary(amcep(choice ~ composition + largest_group, data = data, 
                        tasks = "task_no",
                        id = "ResponseID"))$coefficients)

#label variables
model1_amcep$Variable <- c("60/38/2",
                           "70/28/2",
                           "80/18/2",
                           "90/8/2",
                           "Latino",
                           "Asian",
                           "Black")

# remove z-value and p-value, add upper and lower CI bounds
m1_amcep_table <- select(model1_amcep, Variable, Estimate, Std..Error) %>% 
                  mutate(Upper = Estimate + 1.96*Std..Error,
                         Lower = Estimate - 1.96*Std..Error)

# add reference group levels and attribute names
m1_amcep_table <- rbind(m1_amcep_table,
                        c("50/48/2", 0, 0, 0, 0),
                        c("White", 0, 0, 0, 0))

# reorder and change data type
m1_amcep_table$Variable <- as.factor(m1_amcep_table$Variable)
m1_amcep_table$Variable <- fct_relevel(m1_amcep_table$Variable,
                                        "90/8/2",
                                        "80/18/2",
                                        "70/28/2",
                                        "60/38/2",
                                        "50/48/2",
                                       "Black",
                                       "Asian",
                                       "Latino",
                                       "White")
m1_amcep_table <- m1_amcep_table %>% arrange(Variable)  %>% 
                                     mutate(Attribute = as.factor(c(rep("Heterogeneity", 5), rep("Largest Group", 4))))
m1_amcep_table[ ,2:5] <- sapply(m1_amcep_table[, 2:5], FUN = as.numeric)


# plot
AMCEP_1 <- ggplot(m1_amcep_table, aes(y = Estimate, x = Variable)) +
  ylim(-0.6,0.3) +
  coord_flip() +
  geom_hline(yintercept = 0, size = 0.3, linetype = "dotted") +
  geom_pointrange(aes(ymin = Lower, ymax = Upper),
                  position = position_dodge(width = 0.9),
                  size = 0.3) +
  theme_bw() +
  labs(x = "", y = "AMCEP") +
  facet_wrap(~Attribute, scale = "free_y", ncol = 1) 

ggsave("amcep_1.pdf", AMCEP_1, dpi = 600)
```

\newpage

## Heterogeneity and Absent Group 

```{r, echo = F, warning = F, message = F, fig.align= "center", fig.width = 3.75, fig.height = 4.75}
# estimate and store coefficients in dataframe
model2_amcep <- data.frame(summary(amcep(choice ~ composition + omitted_group, data = data, 
                        tasks = "task_no",
                        id = "ResponseID"))$coefficients)

#label variables
model2_amcep$Variable <- c("60/38/2",
                           "70/28/2",
                           "80/18/2",
                           "90/8/2",
                           "Latino",
                           "Asian",
                           "Black")

# remove z-value and p-value, add upper and lower CI bounds
m2_amcep_table <- select(model2_amcep, Variable, Estimate, Std..Error) %>% 
                  mutate(Upper = Estimate + 1.96*Std..Error,
                         Lower = Estimate - 1.96*Std..Error)

# add reference group levels and attribute names
m2_amcep_table <- rbind(m2_amcep_table,
                        c("50/48/2", 0, 0, 0, 0),
                        c("White", 0, 0, 0, 0))

# reorder and change data type
m2_amcep_table$Variable <- as.factor(m2_amcep_table$Variable)
m2_amcep_table$Variable <- fct_relevel(m2_amcep_table$Variable,
                                        "90/8/2",
                                        "80/18/2",
                                        "70/28/2",
                                        "60/38/2",
                                        "50/48/2",
                                        "Black",
                                        "Asian",
                                        "Latino",
                                        "White")
m2_amcep_table <- m2_amcep_table %>% arrange(Variable) %>% 
                            mutate(Attribute = as.factor(c(rep("Heterogeneity", 5), rep("Absent Group", 4))))
m2_amcep_table$Attribute <- fct_relevel(m2_amcep_table$Attribute, "Heterogeneity", "Absent Group")
m2_amcep_table[ ,2:5] <- sapply(m2_amcep_table[, 2:5], FUN = as.numeric)            

# plot
AMCEP_2 <- ggplot(m2_amcep_table, aes(y = Estimate, x = Variable)) +
  ylim(-0.6,0.3) +
  coord_flip() +
  geom_hline(yintercept = 0, size = 0.3, linetype = "dotted") +
  geom_pointrange(aes(ymin = Lower, ymax = Upper),
                  position = position_dodge(width = 0.9),
                  size = 0.3) +
  theme_bw() +
  labs(x = "", y = "AMCEP") +
  facet_wrap(~Attribute, scale = "free_y", ncol = 1) 

ggsave("amcep_2.pdf", AMCEP_2, dpi = 600)


# alternative image with models 1 and 2 together:
amcep_1_2 <- AMCEP_1 + AMCEP_2
amcep_1_2
ggsave("amcep_1_2_alt.pdf", amcep_1_2, dpi = 600)
```

\newpage

## Heterogeneity and Largest Group by Respondent Race

```{r, echo = F, warning = F, message = F, fig.align= "center", fig.width = 7.5, fig.height = 9.5}
#subset by race
whites <- filter(data, race == "White")
blacks <- filter(data, race == "Black/African American")
latinos <- filter(data, race == "Hispanic/Latino(a)")
asians <- filter(data, race == "Asian/Asian American")

# estimate and store coefficients in dataframe
model1_amcep_whites <- data.frame(summary(amcep(choice ~ composition + largest_group, data = whites, 
                        tasks = "task_no",
                        id = "ResponseID"))$coefficients)
model1_amcep_blacks <- data.frame(summary(amcep(choice ~ composition + largest_group, data = blacks, 
                        tasks = "task_no",
                        id = "ResponseID"))$coefficients)
model1_amcep_asians <- data.frame(summary(amcep(choice ~ composition + largest_group, data = asians, 
                        tasks = "task_no",
                        id = "ResponseID"))$coefficients)
model1_amcep_latinos <- data.frame(summary(amcep(choice ~ composition + largest_group, data = latinos, 
                        tasks = "task_no",
                        id = "ResponseID"))$coefficients)

# turn this into a function
plot_amcep_function <- function(model_estimates){
  #create variables labels
  table <- model_estimates %>% mutate(Variable = c("60/38/2",
                                                   "70/28/2",
                                                   "80/18/2",
                                                   "90/8/2",
                                                   "Latino",
                                                   "Asian",
                                                   "Black")) %>% 
 # remove z-value and p-value, add upper and lower CI bounds
 select(Variable, Estimate, Std..Error) %>% 
 mutate(Upper = Estimate + 1.96*Std..Error,
        Lower = Estimate - 1.96*Std..Error) %>% 
 # add reference group levels and attribute names
 rbind(.,c("50/48/2", 0, 0, 0, 0),
         c("White", 0, 0, 0, 0)) %>% 
 # reorder and change data type
 mutate(Variable = as.factor(Variable)) %>% 
 mutate(Variable = fct_relevel(Variable,
                               "90/8/2",
                               "80/18/2",
                               "70/28/2",
                               "60/38/2",
                               "50/48/2",
                               "Black",
                               "Asian",
                               "Latino",
                               "White")) %>% 
  arrange(Variable) %>% 
  mutate(Attribute = as.factor(c(rep("Heterogeneity", 5), rep("Largest Group", 4))))
  table[ ,2:5] <- sapply(table[, 2:5], FUN = as.numeric)
  # plot
plot <- ggplot(table, aes(y = Estimate, x = Variable)) +
  coord_flip() +
  ylim(-0.8, 0.4) +
  geom_hline(yintercept = 0, size = 0.3, linetype = "dotted") +
  geom_pointrange(aes(ymin = Lower, ymax = Upper),
                  position = position_dodge(width = 0.9),
                  size = 0.1) +
  theme_bw() +
  labs(x = "", y = "AMCEP") +
  facet_wrap(~Attribute, scale = "free_y", ncol = 1) +
  theme(axis.text=element_text(size=6),
        axis.title=element_text(size=7),
        strip.text = element_text(size = 7))

return(plot)
}

#plot and arrange
whites_model1 <- plot_amcep_function(model1_amcep_whites) + 
  ggtitle("White Participants") + 
  theme(plot.title = element_text(hjust = 0.5, size = 8))

blacks_model1 <- plot_amcep_function(model1_amcep_blacks) + 
  ggtitle("Black Participants") + 
  theme(plot.title = element_text(hjust = 0.5, size = 8))

asians_model1 <- plot_amcep_function(model1_amcep_asians) + 
  ggtitle("Asian Participants") + 
  theme(plot.title = element_text(hjust = 0.5, size = 8))

latinos_model1 <- plot_amcep_function(model1_amcep_latinos) + 
  ggtitle("Latino Participants") + 
  theme(plot.title = element_text(hjust = 0.5, size = 8))

AMCEP_3 <- whites_model1 + latinos_model1 + asians_model1 + blacks_model1

AMCEP_3

ggsave("amcep_3.pdf", AMCEP_3, dpi = 600)
```

\newpage

## Heterogeneity and Absent Group by Respondent Race

```{r, echo = F, warning = F, message = F, fig.align= "center", fig.width = 7.5, fig.height = 9.5}
# estimate and store coefficients in dataframe
model2_amcep_whites <- data.frame(summary(amcep(choice ~ composition + omitted_group, data = whites, 
                        tasks = "task_no",
                        id = "ResponseID"))$coefficients)
model2_amcep_blacks <- data.frame(summary(amcep(choice ~ composition + omitted_group, data = blacks, 
                        tasks = "task_no",
                        id = "ResponseID"))$coefficients)
model2_amcep_asians <- data.frame(summary(amcep(choice ~ composition + omitted_group, data = asians, 
                        tasks = "task_no",
                        id = "ResponseID"))$coefficients)
model2_amcep_latinos <- data.frame(summary(amcep(choice ~ composition + omitted_group, data = latinos, 
                        tasks = "task_no",
                        id = "ResponseID"))$coefficients)

# turn this into a function
plot_amcep2_function <- function(model_estimates){
  #create variables labels
  table <- model_estimates %>% mutate(Variable = c("60/38/2",
                                                   "70/28/2",
                                                   "80/18/2",
                                                   "90/8/2",
                                                   "Latino",
                                                   "Asian",
                                                   "Black")) %>% 
 # remove z-value and p-value, add upper and lower CI bounds
 select(Variable, Estimate, Std..Error) %>% 
 mutate(Upper = Estimate + 1.96*Std..Error,
        Lower = Estimate - 1.96*Std..Error) %>% 
 # add reference group levels and attribute names
 rbind(.,c("50/48/2", 0, 0, 0, 0),
         c("White", 0, 0, 0, 0)) %>% 
 # reorder and change data type
 mutate(Variable = as.factor(Variable)) %>% 
 mutate(Variable = fct_relevel(Variable,
                               "90/8/2",
                               "80/18/2",
                               "70/28/2",
                               "60/38/2",
                               "50/48/2",
                               "Black",
                               "Asian",
                               "Latino",
                               "White")) %>% 
  arrange(Variable) %>% 
  mutate(Attribute = as.factor(c(rep("Heterogeneity", 5), rep("Absent Group", 4)))) %>% 
  mutate(Attribute = fct_relevel(Attribute, "Heterogeneity", "Absent Group")) 
  table[ ,2:5] <- sapply(table[, 2:5], FUN = as.numeric)
  # plot
  plot <- ggplot(table, aes(y = Estimate, x = Variable)) +
  ylim(-0.8,0.4) +
  coord_flip() +
  geom_hline(yintercept = 0, size = 0.3, linetype = "dotted") +
  geom_pointrange(aes(ymin = Lower, ymax = Upper),
                  position = position_dodge(width = 0.9),
                  size = 0.1) +
  theme_bw() +
  labs(x = "", y = "AMCEP") +
  facet_wrap(~Attribute, scale = "free_y", ncol = 1) +
    theme(axis.text=element_text(size=6),
        axis.title=element_text(size=7),
        strip.text = element_text(size = 7))

return(plot)
}

#plot and arrange
whites_model2 <- plot_amcep2_function(model2_amcep_whites) + 
  ggtitle("White Participants") + 
  theme(plot.title = element_text(hjust = 0.5, size = 8))

blacks_model2 <- plot_amcep2_function(model2_amcep_blacks) + 
  ggtitle("Black Participants") + 
  theme(plot.title = element_text(hjust = 0.5, size = 8))

asians_model2 <- plot_amcep2_function(model2_amcep_asians) + 
  ggtitle("Asian Participants") + 
  theme(plot.title = element_text(hjust = 0.5, size = 8))

latinos_model2 <- plot_amcep2_function(model2_amcep_latinos) + 
  ggtitle("Latino Participants") + 
  theme(plot.title = element_text(hjust = 0.5, size = 8))

AMCEP_4 <- whites_model2 + latinos_model2 + asians_model2 + blacks_model2

AMCEP_4

ggsave("amcep_4.pdf", AMCEP_4, dpi = 600)
```

\newpage

## Heterogeneity and Largest Group for Whites with Different Attitudes towards Affirmative Action

```{r, echo = F, warning = F, message = F, fig.align= "center"}
#subset by race & attitude
lib_whites <- filter(data, race == "White" & aa_factor == "Strongly favor")
con_whites <- filter(data, race == "White" & aa_factor == "Strongly oppose")

# estimate and store coefficients in dataframe
model1_amcep_libs <- data.frame(summary(amcep(choice ~ composition + largest_group, data = lib_whites, 
                        tasks = "task_no",
                        id = "ResponseID"))$coefficients)
model1_amcep_cons <- data.frame(summary(amcep(choice ~ composition + largest_group, data = con_whites, 
                        tasks = "task_no",
                        id = "ResponseID"))$coefficients)

#plot and arrange
lib_whites_model1 <- plot_amcep_function(model1_amcep_libs) + 
  ggtitle(str_wrap("White Participants Strongly Favoring Affirmative Action", width = 40)) + 
  theme(plot.title = element_text(hjust = 0.5, size = 8))

con_whites_model1 <- plot_amcep_function(model1_amcep_cons) + 
  ggtitle(str_wrap("White Participants Strongly Opposing Affirmative Action", width = 40)) + 
  theme(plot.title = element_text(hjust = 0.5, size = 8))

AMCEP_5 <- lib_whites_model1 + con_whites_model1
AMCEP_5
ggsave("amcep_5.pdf", AMCEP_5, dpi = 600)
```

\newpage

## Heterogeneity and Largest Group for Whites with Different Attitudes towards Immigration
```{r, echo = F, warning = F, message = F, fig.align= "center"}
#subset by race & attitude
lib_whites2 <- filter(data, race == "White" & immigration_factor == "Increased")
con_whites2 <- filter(data, race == "White" & immigration_factor == "Decreased")

# estimate and store coefficients in dataframe
model1_amcep_libs2 <- data.frame(summary(amcep(choice ~ composition + largest_group, data = lib_whites2, 
                        tasks = "task_no",
                        id = "ResponseId"))$coefficients)
model1_amcep_cons2 <- data.frame(summary(amcep(choice ~ composition + largest_group, data = con_whites2, 
                        tasks = "task_no",
                        id = "ResponseId"))$coefficients)

#plot and arrange
lib_whites_model1_2 <- plot_amcep_function(model1_amcep_libs2) + 
  ggtitle(str_wrap("White Participants who think Immigration should be Increased", width = 36)) + 
  theme(plot.title = element_text(hjust = 0.5, size = 8))

con_whites_model1_2 <- plot_amcep_function(model1_amcep_cons2) + 
  ggtitle(str_wrap("White Participants who think Immigration should be Decreased", width = 36)) + 
  theme(plot.title = element_text(hjust = 0.5, size = 8))

AMCEP_6 <- lib_whites_model1_2 + con_whites_model1_2
AMCEP_6
ggsave("amcep_6.pdf", AMCEP_6, dpi = 600)
```

\newpage

# Alternative Specification of Attitudinal Variables (White Respondents)

If attitudes towards affirmative action and immigration were coded as continuous (0 = oppose/decrease)

## Affirmative Action

```{r, collapse = T, message = F, results = "asis", echo = F, eval = F}
# no controls model 
aa_continuous <- lm(rating~ composition + aa_continuous*largest_group, data = whites)
# controls
aa_continuous_controls <- lm(rating~ composition + aa_continuous*largest_group +
                       age + gender + educ + inc_num + born, data = whites)

stargazer(aa_continuous, aa_continuous_controls,
          title = "Whites' Affirmative Action Attitudes (Continuous) and Identity of Largest Group",
          se = list(clSE(aa_continuous), clSE(aa_continuous_controls)), 
          header = F, digits = 3, 
          t.auto = F, p.auto = F,
          no.space = T, single.row = T,
          star.cutoffs = c(0.05, 0.01, 0.001),
          order = c(1:4, 6:8, 5, 19:21, 9:18, 22),
          dep.var.labels.include = F,
          dep.var.caption = "",
          intercept.bottom = T,
          covariate.labels = c("60/38/2", 
                               "70/28/2",
                               "80/18/2",
                               "90/8/2",
                               "Latinos",
                               "Asians",
                               "Blacks",
                               "More in favor of Aff.Action",
                               "Latino largest * More in favor of Aff.Action",
                               "Asian largest * More in favor of Aff.Action",
                               "Black largest * More in favor of Aff.Action",
                               "Age",
                               "Male",
                               "High school/GED",
                               "2-year degree (Associate's)",
                               "Some college",
                               "4-year degree (Bachelor's)",
                               "Graduate or professional degree",
                               "Family Income (in thousands)",
                               "Born in U.S. territory (e.g. Puerto Rico)",
                               "Born in some other country"),
          omit.stat = "f",
          add.lines = list(c("F Statistic", F_stat(aa_continuous), F_stat(aa_continuous_controls))))
```


\newpage
## Immigration

```{r, collapse = T, message = F, results = "asis", echo = F, eval = F}
# no controls model 
immigration_continuous <- lm(rating~ composition + immigration_continuous*largest_group, data = whites)
# controls
immigration_continuous_controls <- lm(rating~ composition + immigration_continuous*largest_group +
                       age + gender + educ + inc_num + born, data = whites)

stargazer(immigration_continuous, immigration_continuous_controls,
          title = "Whites' Immigration Attitudes (Continuous) and Identity of Largest Group",
          se = list(clSE(immigration_continuous), clSE(immigration_continuous_controls)), 
          header = F, digits = 3, 
          t.auto = F, p.auto = F,
          no.space = T, single.row = T,
          star.cutoffs = c(0.05, 0.01, 0.001),
          order = c(1:4, 6:8, 5, 19:21, 9:18, 22),
          dep.var.labels.include = F,
          dep.var.caption = "",
          intercept.bottom = T,
          covariate.labels = c("60/38/2", 
                               "70/28/2",
                               "80/18/2",
                               "90/8/2",
                               "Latinos",
                               "Asians",
                               "Blacks",
                               "Wants more immigration",
                               "Latino largest * Wants more immigration",
                               "Asian largest * Wants more immigration",
                               "Black largest * Wants more immigration",
                               "Age",
                               "Male",
                               "High school/GED",
                               "2-year degree (Associate's)",
                               "Some college",
                               "4-year degree (Bachelor's)",
                               "Graduate or professional degree",
                               "Family Income (in thousands)",
                               "Born in U.S. territory (e.g. Puerto Rico)",
                               "Born in some other country"),
          omit.stat = "f",
          add.lines = list(c("F Statistic", F_stat(immigration_continuous), F_stat(immigration_continuous_controls))))
```

\newpage

# Straightliners

```{r, echo = F, warning = F, message = F}
# load dataset containing straightliners
# set working directory:
straightliners <- readRDS("with_straightliners.rds")

# reorder levels of largest group, omitted group and R race variables
straightliners$largest_group <- fct_relevel(straightliners$largest_group,
                                  c("White",
                                    "Latino",
                                    "Asian",
                                    "Black"))
straightliners$omitted_group <- fct_relevel(straightliners$omitted_group,
                                  c("White",
                                    "Latino",
                                    "Asian",
                                    "Black"))
straightliners$race <- fct_relevel(straightliners$race,
                         c("White",
                           "Hispanic/Latino(a)",
                           "Asian/Asian American",
                           "Black/African American"))
```

## Heterogeneity and Largest Group

```{r, results = "asis", message = F, echo = F, eval = F}
model1 <- lm(rating ~ composition + largest_group, straightliners)
# with controls
model1_controls <- lm(rating ~ composition + largest_group + race +
                        age + gender + educ + inc_num + born, straightliners)

# table
stargazer(model1, model1_controls,
          title = "Heterogeneity + Largest Group (Including Straightliners)",
          se = list(clSE(model1), clSE(model1_controls)),
          header = F, digits = 3,
          t.auto = F, p.auto = F, no.space = T, single.row = T,
          star.cutoffs = c(0.05, 0.01, 0.001),
          covariate.labels = c("60/38/2", 
                               "70/28/2",
                               "80/18/2",
                               "90/8/2",
                               "Latinos",
                               "Asians",
                               "Blacks",
                               "Latino",
                               "Asian",
                               "Black",
                               "Age",
                               "Male",
                               "High school/GED",
                               "2-year degree (Associate's)",
                               "Some college",
                               "4-year degree (Bachelor's)",
                               "Graduate or professional degree",
                               "Family Income (in thousands)",
                               "Born in U.S. territory (e.g. Puerto Rico)",
                               "Born in some other country"),
          omit.stat = "f",
          add.lines = list(c("F Statistic", F_stat(model1), F_stat(model1_controls))))
```

\newpage
## Heterogeneity and Absent Group

```{r, collapse = T, message = F, results = "asis", echo = F, eval = F}
model2 <- lm(rating ~ composition + omitted_group, data = straightliners)
# with controls
model2_controls <- lm(rating ~ composition + omitted_group + race +
                        age + gender + educ + inc_num + born, data = straightliners)

stargazer(model2, model2_controls,
          title = "Heterogeneity + Absent Group (Including Straightliners)",
          se = list(clSE(model2), clSE(model2_controls)),
          header = F, digits = 3, intercept.bottom = T, 
          t.auto = F, p.auto = F, no.space = T, single.row = T,
          star.cutoffs = c(0.05, 0.01, 0.001),
          covariate.labels = c("60/38/2", 
                               "70/28/2",
                               "80/18/2",
                               "90/8/2",
                               "Latinos",
                               "Asians",
                               "Blacks",
                               "Latino",
                               "Asian",
                               "Black",
                               "Age",
                               "Male",
                               "High school/GED",
                               "2-year degree (Associate's)",
                               "Some college",
                               "4-year degree (Bachelor's)",
                               "Graduate or professional degree",
                               "Family Income (in thousands)",
                               "Born in U.S. territory (e.g. Puerto Rico)",
                               "Born in some other country"),
          omit.stat = "f",
          add.lines = list(c("F Statistic", F_stat(model2), F_stat(model2_controls))))
```
